package it.rikesh.aip2011.client.component;

import it.rikesh.aip2011.client.BaseLayout;
import it.rikesh.aip2011.client.service.FaunaFloraService;
import it.rikesh.aip2011.client.service.FaunaFloraServiceAsync;
import it.rikesh.aip2011.shared.model.Trail;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

/**
 * @author  sunita
 */
public class FaunaFloraLayout extends LayoutContainer {
    private static FaunaFloraServiceAsync faunaService = GWT.create(FaunaFloraService.class);
    
    private Text txtName;
    private Text txtDescription;
    private Text lblDescription;
    private Text lblTrail;
    private List<Trail> trails;
    private HTML image;
    private FlexTable tableTr;
    private String id;
    
    public FaunaFloraLayout(String name,boolean link){
        faunaService.getFaunaId(name, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                id=result;
                init();
                getInfoFFauna(id);
                
                addComponent();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
    }
    
    public FaunaFloraLayout(String id){
        init();
        getInfoFFauna(id);
        
        addComponent();
    }
    
    /**
     * Questo metodo si occupa di inizializzare tutte le variabili necessari
     */
    public void init(){
        lblDescription=new Text("DESCRIZIONE");
        lblDescription.setStyleAttribute("color", "red");
        lblDescription.setStyleAttribute("text-align", "center");
        lblDescription.setStyleAttribute("font-weight", "bold");
        lblDescription.setStyleAttribute("font-size", "18px");
        lblTrail=new Text("SENTIERI PERCORRIBILI");
        lblTrail.setStyleAttribute("color", "red");
        lblTrail.setStyleAttribute("text-align", "center");
        lblTrail.setStyleAttribute("font-weight", "bold");
        lblTrail.setStyleAttribute("font-size", "18px");
        txtName=new Text();
        txtName.setStyleAttribute("color", "red");
        txtName.setStyleAttribute("text-align", "center");
        txtName.setStyleAttribute("font-weight", "bold");
        txtName.setStyleAttribute("font-size", "35px");
        txtName.setStyleAttribute("font-family", "Rock Salt");
        txtDescription=new Text();
        txtDescription.setStyleAttribute("text-align", "center");
        image=new HTML();
        tableTr=new FlexTable();
    }
    
    /**
     * Permettere di ottenere tutti dati nel datatstore relativo al rifugio
     * @param id id del rifugio da visualizzare
     */
    private void getInfoFFauna(String id){
        faunaService.getTrailFauna(id, new AsyncCallback<List<Trail>>() {
            
            public void onSuccess(List<Trail> result) {
                trails=new ArrayList<Trail>();
                for (int i=0;i<result.size();i++) {
                    trails.add(result.get(i));
                    Text tr=new Text(result.get(i).getName());
                    tr.setStyleAttribute("text-decoration", "underline");
                    tr.setStyleAttribute("color", "blue");
                    tr.addListener(Events.OnClick, new Listener<BaseEvent>() {
                        
                        public void handleEvent(BaseEvent be) {
                            BaseLayout.changeContent(new TrailLayout(((Text)be.getSource()).getText(),true));
                        }
                    });
                    tableTr.setWidget(i, 0, tr);
                }
                layout();
                
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        faunaService.getFaunaName(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtName.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        faunaService.getFaunaDetail(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtDescription.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        faunaService.getFaunaImg(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                image.setHTML("<center><img src='images/ffauna/"+result+"'/></center>");
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
    }
    
    private void addComponent(){
        add(txtName);
        add(image);
        add(lblDescription);
        add(txtDescription);
        add(lblTrail);
        add(tableTr);
    }
}
